tdf#72942 Do not overwrite styles from the inserted doc

During inserting of the text from the external document
we should not change original default styles in
currently opened document.

Added unit test.

Change-Id: Ida754a0da5efaaa043464cd807c0b52cfb0d5670
Reviewed-on: https://gerrit.libreoffice.org/48826
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Thorsten Behrens <Thorsten.Behrens@CIB.de>
diff --git a/sw/qa/extras/rtfexport/rtfexport2.cxx b/sw/qa/extras/rtfexport/rtfexport2.cxx
index efab6c8..a83ba39 100644
--- a/sw/qa/extras/rtfexport/rtfexport2.cxx
+++ b/sw/qa/extras/rtfexport/rtfexport2.cxx
@@ -677,20 +677,25 @@ DECLARE_RTFEXPORT_TEST(testFdo63428, "hello.rtf")
                         getProperty<OUString>(getRun(getParagraph(1), 4), "TextPortionType"));
}

DECLARE_RTFEXPORT_TEST(testFdo69384, "hello.rtf")
#endif

DECLARE_RTFEXPORT_TEST(testFdo69384, "fdo69384-paste.rtf")
{
    // Check if the style is loaded
    getStyles("ParagraphStyles")->getByName("Text body justified");
}

DECLARE_RTFEXPORT_TEST(testFdo69384Inserted, "hello.rtf")
{
    uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
    uno::Reference<text::XTextRange> xText(xTextDocument->getText(), uno::UNO_QUERY);
    uno::Reference<text::XTextRange> xEnd = xText->getEnd();
    paste("fdo69384-paste.rtf", xEnd);

    // Import got interrupted in the middle of style sheet table import,
    // resulting in missing styles and text.
    getStyles("ParagraphStyles")->getByName("Text body justified");
    // During insert of the RTF document we do not insert new styles
    CPPUNIT_ASSERT(!getStyles("ParagraphStyles")->hasByName("Text body justified"));
}

#endif

DECLARE_RTFEXPORT_TEST(testFdo61193, "hello.rtf")
{
    // Pasting content that contained a footnote caused a crash.
diff --git a/sw/qa/extras/uiwriter/data/fdo72942-insert.docx b/sw/qa/extras/uiwriter/data/fdo72942-insert.docx
new file mode 100755
index 0000000..fb1f80c
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data/fdo72942-insert.docx
Binary files differ
diff --git a/sw/qa/extras/uiwriter/data/fdo72942.docx b/sw/qa/extras/uiwriter/data/fdo72942.docx
new file mode 100755
index 0000000..0906ec4
--- /dev/null
+++ b/sw/qa/extras/uiwriter/data/fdo72942.docx
Binary files differ
diff --git a/sw/qa/extras/uiwriter/uiwriter.cxx b/sw/qa/extras/uiwriter/uiwriter.cxx
index 4789d2d..a87abf1 100644
--- a/sw/qa/extras/uiwriter/uiwriter.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter.cxx
@@ -264,6 +264,7 @@ public:
    void testTdf106736();
    void testTdf58604();
    void testTdf112025();
    void testTdf72942();
    void testTdf113877();
    void testTdf113877NoMerge();
    void testMsWordCompTrailingBlanks();
@@ -442,6 +443,7 @@ public:
    CPPUNIT_TEST(testTdf106736);
    CPPUNIT_TEST(testTdf58604);
    CPPUNIT_TEST(testTdf112025);
    CPPUNIT_TEST(testTdf72942);
    CPPUNIT_TEST(testTdf113877);
    CPPUNIT_TEST(testTdf113877NoMerge);
    CPPUNIT_TEST(testMsWordCompTrailingBlanks);
@@ -5279,6 +5281,39 @@ void SwUiWriterTest::testTdf112025()
    CPPUNIT_ASSERT_EQUAL(true, getProperty<bool>(xStyle, "IsLandscape"));
}

void SwUiWriterTest::testTdf72942()
{
    load(DATA_DIRECTORY, "fdo72942.docx");

    // get a page cursor
    uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
    uno::Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier(xModel->getCurrentController(), uno::UNO_QUERY);
    uno::Reference<text::XPageCursor> xCursor(xTextViewCursorSupplier->getViewCursor(), uno::UNO_QUERY);
    xCursor->jumpToEndOfPage();

    OUString insertFileid = m_directories.getURLFromSrc(DATA_DIRECTORY) + "fdo72942-insert.docx";
    uno::Sequence<beans::PropertyValue> aPropertyValues(comphelper::InitPropertySequence({{ "Name", uno::makeAny(insertFileid) }}));
    lcl_dispatchCommand(mxComponent, ".uno:InsertDoc", aPropertyValues);

    // check styles of paragraphs added from [fdo72942.docx]
    const uno::Reference< text::XTextRange > xRun1 = getRun(getParagraph(1), 1);
    CPPUNIT_ASSERT_EQUAL(OUString("Default English (Liberation serif) text with "), xRun1->getString());
    CPPUNIT_ASSERT_EQUAL(OUString("Liberation Serif"), getProperty<OUString>(xRun1, "CharFontName"));

    const uno::Reference< text::XTextRange > xRun2 = getRun(getParagraph(2), 1);
    CPPUNIT_ASSERT_EQUAL(OUString("Header 1 English text (Liberation sans) with "), xRun2->getString());
    CPPUNIT_ASSERT_EQUAL(OUString("Liberation Sans"), getProperty<OUString>(xRun2, "CharFontName"));

    // check styles of paragraphs added from [fdo72942-insert.docx]
    const uno::Reference< text::XTextRange > xRun3 = getRun(getParagraph(4), 1);
    CPPUNIT_ASSERT_EQUAL(OUString("Default German text (Calibri) with "), xRun3->getString());
    CPPUNIT_ASSERT_EQUAL(OUString("Liberation Serif"), getProperty<OUString>(xRun3, "CharFontName"));

    const uno::Reference< text::XTextRange > xRun4 = getRun(getParagraph(5), 1);
    CPPUNIT_ASSERT_EQUAL(OUString("Header 1 German text (Calibri Light) with "), xRun4->getString());
    CPPUNIT_ASSERT_EQUAL(OUString("Liberation Sans"), getProperty<OUString>(xRun4, "CharFontName"));
}

void SwUiWriterTest::testTdf114306()
{
    load(DATA_DIRECTORY, "fdo114306.odt");
diff --git a/sw/source/filter/docx/swdocxreader.cxx b/sw/source/filter/docx/swdocxreader.cxx
index 5a342c9..7a5a644 100644
--- a/sw/source/filter/docx/swdocxreader.cxx
+++ b/sw/source/filter/docx/swdocxreader.cxx
@@ -69,6 +69,13 @@ ErrCode SwDOCXReader::Read(SwDoc& rDoc, const OUString& /* rBaseURL */, SwPaM& r

    const uno::Reference<text::XTextRange> xInsertTextRange = SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), nullptr);
    uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*pMedium->GetInStream()));

    //SetLoading hack because the document properties will be re-initted
    //by the xml filter and during the init, while its considered uninitialized,
    //setting a property will inform the document its modified, which attempts
    //to update the properties, which throws cause the properties are uninitialized
    pDocShell->SetLoading(SfxLoadedFlags::NONE);

    uno::Sequence<beans::PropertyValue> aDescriptor(comphelper::InitPropertySequence(
    {
        { "InputStream", uno::Any(xStream) },
@@ -87,6 +94,8 @@ ErrCode SwDOCXReader::Read(SwDoc& rDoc, const OUString& /* rBaseURL */, SwPaM& r
        SAL_WARN("sw.docx", "SwDOCXReader::Read(): " << e);
        ret = ERR_SWG_READ_ERROR;
    }
    pDocShell->SetLoading(SfxLoadedFlags::ALL);

    return ret;
}

diff --git a/writerfilter/source/dmapper/StyleSheetTable.cxx b/writerfilter/source/dmapper/StyleSheetTable.cxx
index 433405f..b947fbd 100644
--- a/writerfilter/source/dmapper/StyleSheetTable.cxx
+++ b/writerfilter/source/dmapper/StyleSheetTable.cxx
@@ -883,6 +883,13 @@ uno::Sequence< OUString > PropValVector::getNames()

void StyleSheetTable::ApplyStyleSheets( const FontTablePtr& rFontTable )
{
    if (!m_pImpl->m_bIsNewDoc)
    {
        // tdf#72942: do not import styles from document being inserted,
        // while they corrupts original styles in master document
        return;
    }

    try
    {
        uno::Reference< style::XStyleFamiliesSupplier > xStylesSupplier( m_pImpl->m_xTextDocument, uno::UNO_QUERY_THROW );
@@ -1493,6 +1500,14 @@ OUString StyleSheetTable::ConvertStyleName( const OUString& rWWName, bool bExten
void StyleSheetTable::applyDefaults(bool bParaProperties)
{
    try{

        if (!m_pImpl->m_bIsNewDoc)
        {
            // tdf#72942: do not corrupts original styles in master document
            // during inserting of text from second document
            return;
        }

        if(!m_pImpl->m_xTextDefaults.is())
        {
            m_pImpl->m_xTextDefaults.set(